local yxt = require "packages/project_moon/yxt"

local U = require "packages/utility/utility"

local pm_cards = Package:new("pm_u_cards", Package.CardPack)
pm_cards.extensionName = "pm_u_cards"

Fk:loadTranslationTable{
  ["project_moon"] = "月计同人",
  ["pm_u_cards"] = "月计卡包（开）",
  ["pm_ul_cards"] = "月计卡包（关）",
}

local slash = Fk:cloneCard("slash")



local modanSlashSkill = fk.CreateActiveSkill{
  name = "modan__slash_skill",
  prompt = function(self, selected_cards)
    local card = Fk:cloneCard("fire__slash")
    card.subcards = Card:getIdList(selected_cards)
    local max_num = self:getMaxTargetNum(Self, card)
    if max_num > 1 then
      local num = #table.filter(Fk:currentRoom().alive_players, function (p)
        return p ~= Self and not Self:isProhibited(p, card)
      end)
      max_num = math.min(num, max_num)
    end
    card.subcards = {}
    return max_num > 1 and "#fire__slash_skill_multi:::" .. max_num or "#fire__slash_skill"
  end,
  target_num = 1,
  can_use = slash.skill.canUse,
  mod_target_filter = slash.skill.modTargetFilter,
  target_filter = slash.skill.targetFilter,
  on_effect = function(self, room, effect)
    local to = effect.to
    local from = effect.from
    
    local player = room:getPlayerById(from)
    local target = room:getPlayerById(to)

    local num = 1

    
    if player:getMark("@modan_num") == 1 then
      player:chat("「魔弹点火」<br>“这枚魔弹能击中任何人”")
      target:setChainState(true)
      if target.chained then
        local targets = room:getAlivePlayers(true)
        for _, id in ipairs(targets) do
          if id.chained then
            room:addTableMarkIfNeed(player,"modan_damage",id.id)
          end
        end
      end
    elseif player:getMark("@modan_num") == 2 then
      player:chat("「魔弹射击」<br>“这颗子弹绝不会打偏”")
      num = num + 1
    elseif player:getMark("@modan_num") == 3 then
      player:chat("「贯穿魔弹」<br>“沿水平线全部贯穿”")
      local targets = room:getAlivePlayers(true)
      for _, id in ipairs(targets) do
        if id ~= player and id ~= target then
          local sj = id:getNextAlive(false,1,false)
          room:doIndicate(id.id,{sj.id})
          room:damage({
            from = room:getPlayerById(from),
            to = id,
            card = effect.card,
            damage = num,
            damageType = fk.FireDamage,
            skillName = self.name
          })
          room:addTableMarkIfNeed(player,"modan_damage",id.id)
          room:delay(150)
        end
      end
      num = 0
    elseif player:getMark("@modan_num") == 4 then
      player:chat("「噤声魔弹」<br>“挡在我面前者将荡然无存”")
      if not target:hasDelayedTrick("indulgence") then
        local card = Fk:cloneCard("indulgence")
        local pc = room:printCard("modan__slash",Card.Spade,13)
        local id = pc.id
        room:setCardMark(pc, MarkEnum.DestructIntoDiscard, 1)
        card:addSubcard(id)
        target:addVirtualEquip(card)
        room:moveCardTo(card, Player.Judge, target, fk.ReasonJustMove, self.name)
      end
    elseif player:getMark("@modan_num") == 5 then
      player:chat("「魔弹起爆」<br>“只要有这颗子弹,我就无所不能”")
      local targets = room:getAlivePlayers(true)
      local tablea = player:getTableMark("modan_damage")
      for _, id in ipairs(targets) do
        room:doIndicate(from,{id.id})
        if table.contains(tablea,id.id) then
          room:damage({
            from = room:getPlayerById(from),
            to = id,
            card = effect.card,
            damage = num,
            damageType = fk.FireDamage,
            skillName = self.name
          })
          room:addTableMarkIfNeed(player,"modan_damage",id.id)
          room:delay(150)
        end
      end
      num = 0
      -- 6在另外一个技能
    elseif player:getMark("@modan_num") == 7 then
      player:chat("「第七发子弹」<br>“子弹继续向前,永不停歇”")
      num = num + 2
    end
    room:damage({
      from = room:getPlayerById(from),
      to = room:getPlayerById(to),
      card = effect.card,
      damage = num,
      damageType = fk.FireDamage,
      skillName = self.name
    })
    room:addTableMarkIfNeed(player,"modan_damage",to)
    if player:getMark("@modan_num") == 7 then
      room:damage({
        from = room:getPlayerById(from),
        to = room:getPlayerById(from),
        card = effect.card,
        damage = num,
        damageType = fk.FireDamage,
        skillName = self.name
      })
      room:endTurn()
      room:setPlayerMark(player,"@modan_num",0)
      room:setPlayerMark(player,"modan_damage",0)
    end
  end
}

modanSlashSkill:addRelatedSkill(modanAudio)


local modan__slash_trigger = fk.CreateTriggerSkill{
  name = "#modan__slash_trigger",
  global = true,
  mute = true,
  events = {fk.BeforeCardUseEffect},
  can_trigger = function(self, event, target, player, data)
    return data.card.name == "modan__slash" and not player.dead and data.from == player.id
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = math.random(1,2)
    room:broadcastPlaySound("./packages/project_moon/audio/card/modan__slash"..num)
    if player:getMark("@modan_num") ~= 7 then
      room:addPlayerMark(player,"@modan_num",1)
    end

    if player:getMark("@modan_num") == 6 then
      player:chat("「倾泻魔弹」<br>“绝望的心漆黑地燃烧”")
      local card = Fk:cloneCard("modan__slash")
      data.additionalEffect = 2
      data.extra_data = data.extra_data or {}
      data.extra_data.modan_six = data.extra_data.modan_six or {}
      table.insert(data.extra_data.modan_six, target.id)
    elseif player:getMark("@modan_num") == 7 then
      data.disresponsiveList = table.map(room.alive_players, Util.IdMapper)
    end
  end,
}

Fk:addSkill(modan__slash_trigger)



local modan = fk.CreateBasicCard{
  name = "modan__slash",

  number = 13,
  suit = Card.Spade,

  skill = modanSlashSkill,
  is_damage_card = true,
}


pm_cards:addCards{
  modan,
  modan,
  modan,
  modan,
  modan,
  modan,
  modan,
}

Fk:loadTranslationTable{
  ["modan"] = "魔弹",
  ["modan__slash"] = "魔弹",
  ["@modan_num"] = "魔弹",
  [":modan__slash"] = "基本牌，效果同火【杀】且无次数限制，此牌生效时，使用者获得【魔弹】层数且至多为7<br>"..
  "此牌根据魔弹层数效果发生改变<br>"..
  "<font color='purple'>1层：【魔弹点火】</font>命中时，横置目标<br>"..
  "<font color='purple'>2层：【魔弹射击】</font>命中时，使此伤害加1<br>"..
  "<font color='purple'>3层：【贯穿魔弹】</font>命中时，改为对场上除你和目标外所有角色依次造成1点火焰伤害<br>"..
  "<font color='purple'>4层：【噤声魔弹】</font>命中时，将此牌当作【乐不思蜀】置于目标判定区<br>"..
  "<font color='purple'>5层：【魔弹起爆】</font>命中时，改为对所有受到过魔弹伤害的角色造成1点伤害<br>"..
  "<font color='purple'>6层：【倾泻魔弹】</font>此牌重复使用2次，重复使用时不获得【魔弹】层数<br>"..
  "<font color='purple'>7层：【第七发子弹】</font>此牌无法被响应且命中时，改为对目标和自己造成3点伤害。使用后结束本回合，移除所有【魔弹】及记录的伤害<br>",
}

return {
  yxt,
  pm_cards,
}